package com.beiding.sqlbuilder.impl;

import com.beiding.sqlbuilder.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ConditionUtils {

    //
    public static ConditionUnitHolder buildConditionUnitHolder(ConditionUnit conditionUnit, List<ConditionUnitHandler> conditionUnitHandlers) {
        for (ConditionUnitHandler conditionUnitHandler : conditionUnitHandlers) {
            if (conditionUnitHandler.canHandle(conditionUnit)) {
                return new ConditionUnitHolder(conditionUnit, conditionUnitHandler);
            }
        }
        throw new RuntimeException("找不到可以处理的处理器");
    }

    public static ParameterHolder buildParameterHolder(Parameter parameter, List<ParameterHandler> parameterHandlers) {
        for (ParameterHandler parameterHandler : parameterHandlers) {
            if (parameterHandler.canHandle(parameter)) {
                return new ParameterHolder(parameter, parameterHandler);
            }
        }
        throw new RuntimeException("找不到可以处理的处理器");
    }

    public static List<ParameterHolder> buildParameterHolders(Collection<Parameter> parameters, List<ParameterHandler> parameterHandlers) {

        List<ParameterHolder> list = new ArrayList<>();
        for (Parameter parameter : parameters) {
            list.add(buildParameterHolder(parameter, parameterHandlers));
        }
        return list;
    }

    public static void dealConnector(List<ConditionUnit> conditionUnits, LogicType ms) {

        //TODO 不做检查

        //如果收尾为连接符直接报错
        /*ConditionUnit head = conditionUnits.get(0);
        if (head instanceof Connector) {
            throw new RuntimeException("连接符不能开头");
        }
        ConditionUnit tail = conditionUnits.get(conditionUnits.size() - 1);

        if (tail instanceof Connector) {
            throw new RuntimeException("连接符不能结尾");
        }*/

        if (conditionUnits.size() == 1) {
            return;
        }

        //开始从前向后处理
        for (int i = 0; i < conditionUnits.size() - 1; i++) {

            //当前的
            ConditionUnit current = conditionUnits.get(i);
            ConditionUnit next = conditionUnits.get(i + 1);

            if (current instanceof Connector) {
                if (next instanceof Connector) {
                    // throw new RuntimeException("连接符不能联用");
                }
            } else {
                if (!(next instanceof Connector)) {

                    //TODO 测试连接符是否正确
                    SimpleLogicConnector simpleConnector = new SimpleLogicConnector(ms);
                    conditionUnits.add(i + 1, simpleConnector);
                }
            }

        }
    }

    public static Parameter createParameter(Tables queryHolder, Object object) {
        if (object instanceof TableField) {
            TableField tf = (TableField) object;
            queryHolder.addTable(tf.getTable());
            return new SimpleFieldParameter(tf);
        } else if (object instanceof Sentence) {
            return new SimpleSentenceParameter((Sentence) object);
        } else if (object instanceof SelectBuilder) {
            return new SimpleSentenceParameter(((SelectBuilder) object).build());
        } else {
            return new SimpleValueParameter(object);
        }
    }

    public static List<Parameter> createParameters(Tables queryHolder, Object... objects) {
        List<Parameter> parameters = new ArrayList<>();
        for (Object object : objects) {
            parameters.add(createParameter(queryHolder, object));
        }
        return parameters;
    }

    public static List<Parameter> createParameters(Tables queryHolder, Collection objects) {
        List<Parameter> parameters = new ArrayList<>();
        for (Object object : objects) {
            parameters.add(createParameter(queryHolder, object));
        }
        return parameters;
    }


}
